<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;
use Think\Page;

/**
 * 文档基础模型
 */
class ContactsModel extends Model{

    /* 自动验证规则 */
    protected $_validate = array(
        // array('name', '/^[a-zA-Z]\w{0,30}$/', '文档标识不合法', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('name', '', '标识已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
        // array('title', 'require', '标题不能为空', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('category_id', 'require', '分类不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
        // array('category_id', 'require', '分类不能为空', self::EXISTS_VALIDATE , 'regex', self::MODEL_UPDATE),
        // array('category_id,type', 'check_category', '内容类型不正确', self::MUST_VALIDATE , 'function', self::MODEL_INSERT),
        // array('category_id', 'check_category', '该分类不允许发布内容', self::EXISTS_VALIDATE , 'function', self::MODEL_BOTH),
        // array('model_id,category_id,pid', 'check_category_model', '该分类没有绑定当前模型', self::MUST_VALIDATE , 'function', self::MODEL_INSERT),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        // array('uid', 'session', self::MODEL_INSERT, 'function', 'user_auth.uid'),
        // array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        // array('description', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        // array('root', 'getRoot', self::MODEL_BOTH, 'callback'),
        // array('attach', 0, self::MODEL_INSERT),
        // array('view', 0, self::MODEL_INSERT),
        // array('comment', 0, self::MODEL_INSERT),
        // array('extend', 0, self::MODEL_INSERT),
        // array('create_time', NOW_TIME, self::MODEL_INSERT),
        // //array('reply_time', NOW_TIME, self::MODEL_INSERT),
        // array('update_time', NOW_TIME, self::MODEL_BOTH),
        // array('status', 'getStatus', self::MODEL_BOTH, 'callback'),
    );

    public $page = '';

    /**
     * 获取文档列表
     * @param  integer  $category 分类ID
     * @param  string   $order    排序规则
     * @param  integer  $status   状态
     * @param  boolean  $count    是否返回总数
     * @param  string   $field    字段 true-所有字段
     * @return array              文档列表
     */
    

    public function getcontactsByOrderId($order_id){
        $contactsWhere['order_id'] = $order_id;
        $contacts_data = $this
        ->where($contactsWhere)
        ->select();
        return $contacts_data;
    }

    public function getContactNumByOrderId($order_id){
        $contactsWhere['order_id'] = $order_id;
        $num_data = $this
        ->where($contactsWhere)
        ->group('contact_type')
        ->field('contact_type,count(contact_type) as num')
        ->select();
        foreach ($num_data as $key => $value) {
            switch ($value['contact_type']) {
                case '1':
                    $save_data['adult_num'] = intval($value['num']);
                    break;
                case '2':
                    $save_data['child_num'] = intval($value['num']);
                    break;
                case '3':
                    $save_data['childer_num'] = intval($value['num']);
                    break;
                default:
                    # code...
                    break;
            }
        }
        return $save_data;
    }

    public function addContacts($contacts,$order_id){
        foreach ($contacts as $key => $value) {
            $contacts[$key]['order_id'] = $order_id;
        }

        $contacts_data = $this
        ->addAll($contacts);
        if($contacts_data){
            $result = true;
        }else{
            $result = false;
        }
        return $result;
    }
}
